package cn.life.business.notice.controller;

import cn.life.business.notice.entity.po.NoticeUserPo;
import cn.life.common.entity.model.CMessage;
import cn.life.business.notice.entity.po.Notice;
import cn.life.business.notice.entity.vo.NoticeVo;
import cn.life.business.notice.service.NoticeService;
import cn.life.common.entity.model.MyUser;
import cn.life.common.entity.model.MyUserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;


/**
 * @Description:    公告控制层
 * @author wangzhenyu
 * @Date 2018/7/11
 */
@RestController
@RequestMapping("notice")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;


    /**
     * @Description:  公告添加
     * @param   notice
     * @return
     */
    @PostMapping(value = "/addnotice")
    public CMessage addNotice(@RequestBody Notice notice, @MyUser MyUserInfo myUserInfo){
        getOpr(notice,myUserInfo);
      return noticeService.addNotice(notice);
    }

    /**
     * @Description:  公告删除
     * @param   notice
     * @return
     */
    @PostMapping(value = "/deletenotice")
    public CMessage deleteNotice(@RequestBody Notice notice){
        return noticeService.deleteNotice(notice.getNoticeId());
    }
    /**
     * @Description:  修改公告
     * @param   notice
     * @return
     */
    @PostMapping(value = "/updatenotice")
    public CMessage updateNotice(@RequestBody Notice notice){
       return noticeService.updateNotice(notice);
    }
    /**
     * @Description:  查询公告列表
     * @param   noticeVo
     *
     * @return
     */
    @PostMapping(value = "/getallnoticeinfo")
    public CMessage  getAllNoticeInfo(@RequestBody NoticeVo noticeVo, @MyUser MyUserInfo myUserInfo) {
        noticeVo.setOprUserId(myUserInfo.getUserId());
        if(noticeVo.getProjectId() == null){
            noticeVo.setProjectId(myUserInfo.getProjectId());
        }
        return noticeService.getAllNoticeInfo(noticeVo);
    }

    /**
     * @Description:  查询公告详情
     * @param   noticeVo
     *
     * @return
     */
    @PostMapping(value = "/getnotice")
    public  CMessage getNotice(@RequestBody NoticeVo noticeVo, @MyUser MyUserInfo myUserInfo){
        return noticeService.getNotice(noticeVo,myUserInfo);
    }

    /**
     * @Description:  自动添加项
     * @param   notice,myUserInfo
     * @return
     */
    public Notice getOpr(Notice notice, MyUserInfo myUserInfo){
        if(notice.getProjectId() == null || notice.getProjectId().equals("")){
            notice.setProjectId(myUserInfo.getProjectId());
        }
        notice.setOprUserId(myUserInfo.getUserId());
        notice.setOprUserName(myUserInfo.getUserName());
        notice.setOprDate(new Date());
        return notice;
    }



    /**
     * @Description:  修改公告状态（已读、未读）
     * @param   noticeUserPo
     * @return
     */
    @PostMapping(value = "/updatenoticestate")
    public CMessage updateNoticeState(@RequestBody NoticeUserPo noticeUserPo){
        return noticeService.updateNoticeState(noticeUserPo);
    }
}
